<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父级分类" prop="parentId">
        <el-select v-model="queryParams.parentId" clearable  filterable  placeholder="父级分类">
          <el-option
            v-for="dict in parentClassList"
            :key="dict.id"
            :label="dict.title"
            :value="parseInt(dict.id)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:course:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:course:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:course:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:course:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类目表id" align="center" prop="id" v-if="false"/>
      <el-table-column label="分类名称" align="center" prop="title" />
      <el-table-column label="父级分类" align="center" prop="parentName" />
      <el-table-column label="标签地址" align="center" prop="iconUrl" >
        <template slot-scope="scope">
          <img :src="scope.row.iconUrl" alt="标签地址" width="20%" height="20%" @click="showImg(scope.row.iconUrl)"  v-if="scope.row.iconUrl">
        </template>
      </el-table-column>
      <el-table-column label="图片地址" align="center" prop="picUrl" >
        <template slot-scope="scope">
          <img :src="scope.row.picUrl" alt="图片地址" width="20%" height="20%" @click="showImg(scope.row.picUrl)" v-if="scope.row.picUrl">
        </template>
      </el-table-column>
      <el-table-column label="分类等级" align="center" prop="grade" :formatter="gradeFormat" />
      <el-table-column label="排序" align="center" prop="sort" sortable/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:course:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete" v-if="scope.row.id > 4"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:course:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!--  查看图片详情  -->
    <el-dialog :title="imgTitle" :visible.sync="imgOpen" width="30%" append-to-body>
      <div>
        <img :src="imgUrl" alt="" style="width: 100%;height:100%  " />
      </div>
    </el-dialog>
    <!-- 添加或修改产品分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"   label-width="120px">
        <el-form-item label="分类名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="父级分类" prop="parentId">
          <el-select v-model="form.parentId" clearable filterable  placeholder="父级分类" style="width: 100%">
            <el-option
              v-for="dict in parentClassList"
              :key="dict.id"
              :label="dict.title"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" placeholder="请输入排序" style="width: 100%"/>
        </el-form-item>
        <el-form-item label="标签地址" prop="iconUrl">
          <upload-img v-model="form.iconUrl" :limit="1"></upload-img>
        </el-form-item>
        <el-form-item label="图片地址" prop="picUrl">
          <upload-img v-model="form.picUrl" :limit="1"></upload-img>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourse, getCourse, delCourse, addCourse, updateCourse, exportCourse,parentClassList } from "@/api/system/course";
import UploadImg from "@/components/UploadImg";

export default {
  name: "Course",
  components: {
    UploadImg
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品分类表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      //根据类型获取的一级分类列表
      parentClassList: [],
      imgOpen: false,
      imgTitle: "",
      imgUrl: "",
      // 是否显示弹出层
      open: false,
      // 分类等级字典
      gradeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        parentId: undefined,
        iconUrl: undefined,
        picUrl: undefined,
        grade: undefined,
        sort: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getParentClassList();
    this.getDicts("course_grade").then(response => {
      this.gradeOptions = response.data;
    });
  },
  methods: {
    /** 查询产品分类列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取父级分类列表
    getParentClassList(){
      this.form.parentId = null;
      this.queryParams.parentId = null;
      var data = {
        parentId:0,
        grade:1
      };
      parentClassList(data).then(response => {
        this.parentClassList = response.data;
      });
    },
    // 分类等级字典翻译
    gradeFormat(row, column) {
      return this.selectDictLabel(this.gradeOptions, row.grade);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.imgOpen = false;
      this.reset();
    },
    //显示图片
    showImg(imgUrl) {
      this.imgOpen = true;
      this.imgTitle = "图片详情";
      this.imgUrl = imgUrl;
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        parentId: undefined,
        iconUrl: undefined,
        picUrl: undefined,
        grade: undefined,
        sort: undefined,
        createtime: undefined,
        updatetime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getCourse(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改产品分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (this.form.parentId == 0 || this.form.parentId == null){
          this.form.parentId = 0;
          this.form.grade = 1;//一级分类
        }else {
          this.form.grade = 2;//二级分类
        }
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCourse(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除产品分类编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delCourse(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品分类数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCourse(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
